﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCHHoaLan
{
    public partial class FrmQuanLyHangGui : Form
    {
        public FrmQuanLyHangGui()
        {
            InitializeComponent();
        }
        int maHG;
        private void FrmQuanLyHangGui_Load(object sender, EventArgs e)
        {
            txtNgay.Text = DateTime.Today.Day.ToString() + "/" + DateTime.Today.Month.ToString() + "/" + DateTime.Today.Year.ToString();
            HangGuiBUS bus = new HangGuiBUS();
            load();

            // load ten nha cung cap du theo mancc
            cbbMaNCC.DataSource = bus.loadma();
            cbbMaNCC.DisplayMember = "TenNCC";
            cbbMaNCC.ValueMember = "MaNCC";

            //load te hoa dua theo mancc load lan dau tien la 1
            cbbtenhoa.DataSource = bus.loadten(1);
            cbbtenhoa.DisplayMember = "TenHoa";
            cbbtenhoa.ValueMember = "MaHoa";
        }


        public void load()
        {
            HangGuiBUS bus = new HangGuiBUS();
            DataTable dt = bus.loadhang();
            dgvHangGui.DataSource = dt;
        }
        private void cbbMaNCC_SelectedIndexChanged(object sender, EventArgs e)
        {           
            // lam cho ten hoa hien
            HangGuiBUS bus = new HangGuiBUS();            
            string bien = cbbMaNCC.SelectedValue.ToString();
            //rang buoc dieu kien
            if (bien.Length > 10)
            {
                cbbtenhoa.DataSource = bus.loadten(-1);
                cbbtenhoa.Refresh();
            }
            else
            {
                cbbtenhoa.DataSource = bus.loadten(Int32.Parse(bien.ToString()));
                cbbtenhoa.Refresh();
            }
            //--------------------------                  
            cbbtenhoa.DisplayMember = "TenHoa";
            cbbtenhoa.ValueMember = "MaHoa";
            txtsoluong.Text = null;           
           
        }

        private void cbbtenhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            HangGuiBUS bus = new HangGuiBUS();

            DataTable dt = new DataTable();
            dt=bus.loadtextbox(Int32.Parse(cbbtenhoa.SelectedValue.ToString()));
            txtXuatXu.Text = dt.Rows[0][0].ToString();
            txtLoai.Text = dt.Rows[0][1].ToString();           
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Menu main = new Menu();
            main.kq = 1;
            main.Show();
            this.Hide();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbbtenhoa.Text == "")
                {
                    MessageBox.Show("Chưa Chọn Hoa", "Thông Báo", MessageBoxButtons.OK);
                }
                else
                {
                    if (txtsoluong.Text == "")
                    {
                        MessageBox.Show("Chưa Nhập Số Lượng", "Thông Báo", MessageBoxButtons.OK);
                    }
                    else
                    {
                        HangGuiDTO hang = new HangGuiDTO();
                        hang.MaHoa = Int32.Parse(cbbtenhoa.SelectedValue.ToString());
                        hang.MaNCC = Int32.Parse(cbbMaNCC.SelectedValue.ToString());
                        hang.SoLuong = Convert.ToInt32(txtsoluong.Text.ToString());
                        hang.Ngay = DateTime.Today;
                        HangGuiBUS bus = new HangGuiBUS();
                        bus.them(hang);
                        load();
                    }
                }
            }
            catch
            {
                MessageBox.Show("Không thể thêm, nhập lại", "Thông Báo", MessageBoxButtons.OK);
            }
        }      
        
       
        private void btnXoa_Click(object sender, EventArgs e)
        {
            HangGuiBUS bus = new HangGuiBUS();
            bus.xoa(maHG);
            //dgvHangGui.DataSource=null;
            load();
        }

        private void dgvHangGui_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                string k = dgvHangGui.Rows[e.RowIndex].Cells[0].Value.ToString();
                maHG = Int32.Parse(k);
                cbbMaNCC.Text = dgvHangGui.Rows[e.RowIndex].Cells[1].Value.ToString();
                cbbtenhoa.Text = dgvHangGui.Rows[e.RowIndex].Cells[2].Value.ToString();
                txtXuatXu.Text = dgvHangGui.Rows[e.RowIndex].Cells[3].Value.ToString();
                txtLoai.Text = dgvHangGui.Rows[e.RowIndex].Cells[4].Value.ToString();
                DateTime date  = (DateTime)(dgvHangGui.Rows[e.RowIndex].Cells[5].Value);
                txtNgay.Text = date.ToShortDateString();
                txtsoluong.Text = dgvHangGui.Rows[e.RowIndex].Cells[6].Value.ToString();
            }
            else
            {
                return;
            }
        }

        private void btnrefresh_Click(object sender, EventArgs e)
        {
            txtNgay.Text = DateTime.Today.Day.ToString() + "/" + DateTime.Today.Month.ToString() + "/" + DateTime.Today.Year.ToString();
            txtsoluong.Text = null;
        }        
    }
}
